//
//  MyViewController.swift
//  clreader
//
//  Created by colin on 2019/3/27.
//  Copyright © 2019 colin. All rights reserved.
//

import UIKit

/// 个人中心
class MyViewController: CLBaseViewController {

    private lazy var infoTableView = UITableView(frame: CGRect.zero, style: .grouped)
    private let sections:[[CellType]] = [[.用户信息], [.我的订单, .我的收藏], [.商城, .帮助与反馈, .关于我们]]
    
    override func viewDidLoad() {
        super.viewDidLoad()
        self.view.backgroundColor = UIColor.white
        
        let topBar = UIView().then {
            $0.backgroundColor = UIColor.white
            
            let labelTitle = UILabel().then {
                $0.text = "我的"
                $0.textColor = UIColor(hex: "343434")
                $0.font = UIFont.systemFont(ofSize: 19.0, weight: .bold)
            }
            $0.addSubview(labelTitle)
            labelTitle.snp.makeConstraints({ (make) in
                make.left.equalToSuperview().offset(15.0)
                make.centerY.equalToSuperview()
            })
            
            // 扫码
            let buttonSetting = UIButton(type: .custom).then {
                $0.setTitle("设置", for: .normal)
                $0.setTitleColor(UIColor(hex: "828282"), for: .normal)
                $0.titleLabel?.font = UIFont.systemFont(ofSize: 15.0, weight: .light)
                // $0.addTarget(self, action: #selector(self.actionScan(_:)), for: .touchUpInside)
            }
            $0.addSubview(buttonSetting)
            buttonSetting.snp.makeConstraints({ (make) in
                make.top.bottom.equalToSuperview()
                make.right.equalToSuperview()
                make.width.equalTo(44.0)
            })
            
        }
        self.view.addSubview(topBar)
        topBar.snp.makeConstraints { (make) in
            make.left.right.equalToSuperview()
            make.height.equalTo(44.0)
            make.top.equalTo(self.view.safeAreaLayoutGuide.snp.top)
        }
        
        // 注册cell
        self.infoTableView.do {
            $0.register(CellUser.self, forCellReuseIdentifier: CellUser.reuseId)
            $0.register(CellCommon.self, forCellReuseIdentifier: CellCommon.reuseId)
        }
        
        self.infoTableView.do {
            $0.dataSource = self
            $0.delegate = self
        }
        
        self.infoTableView.do {
            $0.contentInsetAdjustmentBehavior = .never
            $0.backgroundColor = UIColor(hex: "efefef")
            $0.estimatedSectionHeaderHeight = CGFloat.leastNormalMagnitude
            $0.estimatedSectionFooterHeight = CGFloat.leastNormalMagnitude
            $0.estimatedRowHeight = CGFloat.leastNormalMagnitude
            $0.sectionHeaderHeight = CGFloat.leastNormalMagnitude
            $0.sectionFooterHeight = CGFloat.leastNormalMagnitude
            $0.tableHeaderView = UIView(frame: CGRect(x: 0, y: 0, width: 0, height: CGFloat.leastNormalMagnitude))
            $0.separatorStyle = .none
            $0.showsVerticalScrollIndicator = false
        }
        
        self.view.addSubview(self.infoTableView)
        self.infoTableView.snp.makeConstraints { (make) in
            make.left.right.equalToSuperview()
            make.top.equalTo(topBar.snp.bottom)
            make.bottom.equalTo(self.view.safeAreaLayoutGuide.snp.bottom)
        }
        
    }

}

extension MyViewController : UITableViewDataSource {
    
    func numberOfSections(in tableView: UITableView) -> Int {
        return self.sections.count
    }
    
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return self.sections[section].count
    }
    
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let  cellType = self.sections[indexPath.section][indexPath.row]
        if cellType == .用户信息 {
            let cell = tableView.dequeueReusableCell(withIdentifier: CellUser.reuseId) as! CellUser
            cell.refresh(nil)
            return cell
        } else {
            let cell = tableView.dequeueReusableCell(withIdentifier: CellCommon.reuseId) as! CellCommon
            cell.refresh(cellType, info: nil)
            return cell
        }
    }
    
}

extension MyViewController : UITableViewDelegate {
    
    func tableView(_ tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
        if section == 0 {
            return CGFloat.leastNormalMagnitude
        } else {
            return 10.0
        }
    }
    
    func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
        let  cellType = self.sections[indexPath.section][indexPath.row]
        if cellType == .用户信息 {
            return CellUser.cellHeight
        } else {
            return CellCommon.cellHeight
        }
    }
    
    func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        let  cellType = self.sections[indexPath.section][indexPath.row]
        print(cellType)
        tableView.deselectRow(at: indexPath, animated: true)
    }
}

extension MyViewController {
    
    enum CellType {
        case 用户信息
        case 我的订单
        case 我的收藏
        case 商城
        case 帮助与反馈
        case 关于我们
        
        var title:String {
            switch self {
            case .用户信息:
                return "用户信息"
            case .我的订单:
                return "我的订单"
            case .我的收藏:
                return "我的收藏"
            case .商城:
                return "商城"
            case .帮助与反馈:
                return "帮助与反馈"
            case .关于我们:
                return "关于我们"
            }
        }
        
        var imageName:String {
            switch self {
            case .用户信息:
                return ""
            case .我的订单:
                return "icon_my_wddd"
            case .我的收藏:
                return "icon_my_wdsc"
            case .商城:
                return "icon_my_sc"
            case .帮助与反馈:
                return "icon_my_bzyfk"
            case .关于我们:
                return "icon_my_gywm"
            }
        }
        
    }
    
    class CellUser: UITableViewCell {
        
        static let cellHeight:CGFloat = 140.0
        static let reuseId = "CellUser"
        
        private lazy var imageViewPhoto = UIImageView()
        private lazy var labelUserName = UILabel()
        
        func refresh(_ userInfo:JSONObject?) {
            
        }
        
        override init(style: UITableViewCell.CellStyle, reuseIdentifier: String?) {
            super.init(style: style, reuseIdentifier: reuseIdentifier)
            self.selectionStyle = .none
            
            self.labelUserName.do {
                $0.textColor = UIColor(hex: "343434")
                $0.font = UIFont.systemFont(ofSize: 15.0, weight: .light)
                $0.textAlignment = .center
            }
            self.contentView.addSubview(self.labelUserName)
            self.labelUserName.snp.makeConstraints { (make) in
                make.left.equalToSuperview().offset(15.0)
                make.right.equalToSuperview().offset(-15.0)
                make.bottom.equalToSuperview().offset(-20.0)
            }
            
            let photoContainer = UIView().then {
                $0.layer.cornerRadius = 37.0
                $0.layer.masksToBounds = true
                
                self.imageViewPhoto.do {
                    $0.layer.cornerRadius = 35.0
                    $0.layer.masksToBounds = true
                }
                $0.addSubview(self.imageViewPhoto)
                self.imageViewPhoto.snp.makeConstraints({ (make) in
                    make.size.equalTo(CGSize(width: 70.0, height: 70.0))
                    make.center.equalToSuperview()
                })
                
            }
            self.contentView.addSubview(photoContainer)
            photoContainer.snp.makeConstraints { (make) in
                make.size.equalTo(CGSize(width: 74.0, height: 74.0))
                make.centerX.equalToSuperview()
                make.bottom.equalTo(self.labelUserName.snp.top).offset(-20.0)
            }
            
        }
        
        required init?(coder aDecoder: NSCoder) {
            fatalError("init(coder:) has not been implemented")
        }
        
    }
    
    class CellCommon: UITableViewCell {
        
        static let cellHeight:CGFloat = 65.0
        static let reuseId = "CellCommon"
        
        private lazy var imageViewIcon = UIImageView()
        private lazy var labelTitle = UILabel()
        
        private lazy var labelCount = UILabel()
        private lazy var labelDes = UILabel()
        
        func refresh(_ type:CellType, info:JSONObject?) {
            self.labelTitle.text = type.title
            self.imageViewIcon.image = UIImage(named: type.imageName)
            
            if type == .我的订单 || type == .我的收藏 {
                self.labelCount.isHidden = false
                self.labelDes.isHidden = false
                
                if type == .我的订单 {
                    self.labelCount.text = "\(7)"
                    self.labelDes.text = "已购\(7)个作品"
                } else if type == .我的收藏 {
                    self.labelCount.text = "\(7)"
                    self.labelDes.text = "已收藏\(7)个作品"
                }
                
                self.accessoryType = .none
            } else {
                self.labelCount.isHidden = false
                self.labelDes.isHidden = false
//                self.labelCount.isHidden = true
//                self.labelDes.isHidden = true
                self.accessoryType = .disclosureIndicator
            }
            
        }
        
        override init(style: UITableViewCell.CellStyle, reuseIdentifier: String?) {
            super.init(style: style, reuseIdentifier: reuseIdentifier)
            
            self.contentView.addSubview(self.imageViewIcon)
            self.imageViewIcon.snp.makeConstraints({ (make) in
                make.size.equalTo(CGSize(width: 22.0, height: 22.0))
                make.left.equalToSuperview().offset(15.0)
                make.centerY.equalToSuperview()
            })
            
            self.labelTitle.do {
                $0.textColor = UIColor(hex: "343434")
                $0.font = UIFont.systemFont(ofSize: 15.0, weight: .light)
            }
            self.contentView.addSubview(self.labelTitle)
            self.labelTitle.snp.makeConstraints { (make) in
                make.left.equalTo(self.imageViewIcon.snp.right).offset(15.0)
                make.centerY.equalToSuperview()
            }
            
            self.labelDes.do {
                $0.textColor = UIColor(hex: "828282")
                $0.font = UIFont.systemFont(ofSize: 12.0, weight: .light)
            }
            self.contentView.addSubview(self.labelDes)
            self.labelDes.snp.makeConstraints { (make) in
                make.right.equalToSuperview().offset(-15.0)
                make.bottom.equalToSuperview().offset(-12.0)
            }
            
            self.labelCount.do {
                $0.textColor = UIColor(hex: "343434")
                $0.font = UIFont.systemFont(ofSize: 17.0, weight: .regular)
            }
            self.contentView.addSubview(self.labelCount)
            self.labelCount.snp.makeConstraints { (make) in
                make.right.equalToSuperview().offset(-15.0)
                make.bottom.equalTo(self.labelDes.snp.top).offset(-8.0)
            }

        }
        
        required init?(coder aDecoder: NSCoder) {
            fatalError("init(coder:) has not been implemented")
        }
        
    }
    
    
}
